#include "VModuleSettingDlg.h"
#include "ui_VModuleSettingDlg.h"

#include <QDialog>
#include <QStringList>
#include <QListWidget>
#include <QListWidgetItem>

#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>

#include "VModuleSettingItemDlg.h"
#include "VFunctions.h"

VModuleSettingDlg::VModuleSettingDlg(QWidget *parent, QJsonArray json_array):
    QDialog(parent),
    ui(new Ui::VModuleSettingDlg)
{
    ui->setupUi(this);
    // 将json_array存储到类变量_result_json_array
    // 以便于返回
    _json_array = json_array;

    foreach(QJsonValue json_value, json_array)
    {
        QJsonObject json_obj = json_value.toObject();
        QString type = json_obj.value("type").toString();
        QString current_text = json_obj.value("current_text").toString();
        QString title = json_obj.value("title").toString();
        QString comment = json_obj.value("comment").toString();


        qDebug() << json_obj.take("title").toString().toLocal8Bit();
        qDebug() << "中文111";

        VModuleSettingItemDlg *dlg;
        if(type == "lineedit")
        {
            dlg = new VModuleSettingItemDlg(title, comment, current_text);
        }
        else if(type == "combobox")
        {
            QString item_type = json_obj.value("itemtype").toString();

            QStringList list;
            if(item_type == "range")
            {
                QString items = json_obj.value("items").toString();
                QStringList reanges = items.split("|");
                foreach(QString range_str, reanges)
                {
                    QString prefix = json_obj.value("prefix").toString();
                    QString subfix = json_obj.value("subfix").toString();
                    QStringList range = range_str.split("-");
                    if(range.count() != 2)
                    {
                        // error
                    }

                    int min = range.at(0).toInt();
                    int max = range.at(1).toInt();
                    for(int i = min; i <= max; i++)
                    {
                        list.append(prefix + QString::number(i) + subfix);
                    }

                    dlg = new VModuleSettingItemDlg(title, comment, list, current_text);
                }
            }
            else if(item_type == "normal")
            {
                QString items = json_obj.value("items").toString();
                list = items.split("|");
                dlg = new VModuleSettingItemDlg(title, comment, list, current_text);
            }
            else
            {
                // error
            }
        }
        else
        {
            // error
        }

        QListWidgetItem *item = new QListWidgetItem(ui->listWidget_moudle_setting);

         item->setSizeHint(QSize(80, 50));
        ui->listWidget_moudle_setting->setItemWidget(item, dlg);

        _dlg_list.append(dlg);

    }

    ui->listWidget_moudle_setting->setWordWrap(true);

}

VModuleSettingDlg::~VModuleSettingDlg()
{
    delete ui;
}

void VModuleSettingDlg::on_pushButton_clicked()
{
    // 确定按钮
    for(int i = 0; i < _json_array.size(); i++)
    {
        QJsonObject json_obj = _json_array.at(i).toObject();
        QString new_text = ((VModuleSettingItemDlg*)(ui->listWidget_moudle_setting->
                                                     itemWidget(ui->listWidget_moudle_setting->item(i))))->get_value();
        // 数据校验

        json_obj.insert("new_text", QJsonValue(new_text));
        _result_json_array.append(QJsonValue(json_obj));
    }
    _is_modify = true;
    this->close();
}
